/*
 * Function test of qtsm_lib_api using glib framework
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include "gtest_qtsm_lib_comm.h"

static comm_req g_lock_pcrs_reqs[] = {
    /* Index: all valid */
    {
        .reqmsg = {
            .type = LOCK_PCRS,
            .qtsm_message_req_u = {
                .lock_pcrs = {
                    .count = 3,
                    .range = (uint16_t[3]){0, 1, 31}
                }
            }
        },
        .resmsg = {
            .type = LOCK_PCRS,
            .qtsm_message_res_u = {
                .lock_pcrs = {
                    .locked = 1
                }
            }
        }
    },
    /* Index: not all valid */
    {
        .reqmsg = {
            .type = LOCK_PCRS,
            .qtsm_message_req_u = {
                .lock_pcrs = {
                    .count = 2,
                    .range = (uint16_t[2]){2, 32}
                }
            }
        },
        .resmsg = {
            .type = ERROR_CODE,
            .qtsm_message_res_u = {
                .error_num = INVALID_INDEX
            }
        }
    },
    /* Index: repeat lock */
    {
        .reqmsg = {
            .type = LOCK_PCRS,
            .qtsm_message_req_u = {
                .lock_pcrs = {
                    .count = 3,
                    .range = (uint16_t[3]){0, 1, 3}
                }
            }
        },
        .resmsg = {
            .type = LOCK_PCRS,
            .qtsm_message_res_u = {
                .lock_pcrs = {
                    .locked = 1
                }
            }
        }
    },
    /* count:0 means range: null */
    {
        .reqmsg = {
            .type = LOCK_PCRS,
            .qtsm_message_req_u = {
                .lock_pcrs = {
                    .count = 0,
                    .range = NULL
                }
            }
        },
        .resmsg = {
            .type = ERROR_CODE,
            .qtsm_message_res_u = {
                .error_num = INVALID_ARGUMENT
            }
        }
    }
};

void gtest_qtsm_lock_pcrs(const void *qtsm_dev_fd)
{
    uint32_t count;
    uint16_t *range = NULL;
    bool locked;
    unsigned int i;
    int rc;

    for (i = 0; i < ARRAY_SIZE(g_lock_pcrs_reqs); i++) {
        count = g_lock_pcrs_reqs[i].reqmsg.qtsm_message_req_u.lock_pcrs.count;
        range = g_lock_pcrs_reqs[i].reqmsg.qtsm_message_req_u.lock_pcrs.range;

        /* test lock pcrs interface */
        rc = qtsm_lock_pcrs(*(int *)qtsm_dev_fd, count, range, &locked);

        if (g_lock_pcrs_reqs[i].resmsg.type == ERROR_CODE) {
            /* check err code */
            g_assert_cmpint(rc, ==,
                g_lock_pcrs_reqs[i].resmsg.qtsm_message_res_u.error_num);
        } else {
            /* check ret value */
            g_assert_cmpint(rc, ==, NO_ERROR);
            /* check lock status */
            g_assert_cmpint(locked, ==,
                g_lock_pcrs_reqs[i].resmsg.qtsm_message_res_u.lock_pcrs.locked);
        }
    }
}